package com.aidong.food.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.activity.biz.MstCampaignBiz;
import com.aidong.food.biz.MstFoodBiz;
import com.aidong.food.biz.TrnFoodBuyBiz;
import com.aidong.food.entity.MstFood;
import com.aidong.myprofile.biz.MyAddressBiz;
import com.aidong.util.bus.CodeConsts;
import com.aidong.util.bus.SecurityUtils;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class TrnFoodBuyAction implements ModelDriven<MstFood> {
	
	private MstFood mstFood;
	private int selectStoreId;
	private int selectAddressId;
	private int selectPickUpWay;
	private String recommendCoach;
	
	@Override
	public MstFood getModel() {
		if(mstFood == null){
			mstFood = new MstFood();
		}
		return mstFood;
	}
	private MyAddressBiz myAddressBiz;

	@Autowired
	public void setMyAddressBiz(MyAddressBiz myAddressBiz) {
		this.myAddressBiz = myAddressBiz;
	}
	
	private TrnFoodBuyBiz trnFoodBuyBiz;
	
	private MstCampaignBiz mstCampaignBiz;
	
	private MstFoodBiz mstFoodBiz;
	
	private Map<String, Object> map;

	@Autowired
	public void setTrnFoodBuyBiz(TrnFoodBuyBiz trnFoodBuyBiz) {
		this.trnFoodBuyBiz = trnFoodBuyBiz;
	}

	@Autowired
	public void setMstCampaignBiz(MstCampaignBiz mstCampaignBiz) {
		this.mstCampaignBiz = mstCampaignBiz;
	}

	@Autowired
	public void setMstFoodBiz(MstFoodBiz mstFoodBiz) {
		this.mstFoodBiz = mstFoodBiz;
	}

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}


	public void setSelectStoreId(int selectStoreId) {
		this.selectStoreId = selectStoreId;
	}

	public void setSelectAddressId(int selectAddressId) {
		this.selectAddressId = selectAddressId;
	}

	public void setSelectPickUpWay(int selectPickUpWay) {
		this.selectPickUpWay = selectPickUpWay;
	}

	public void setRecommendCoach(String recommendCoach) {
		this.recommendCoach = recommendCoach;
	}

	/**
	 * 添加餐购买
	 * @return
	 */
	@Action(value="addfoodbuyorder",results={@Result(name="succ",type="json",params={"root","map"})})
	public String addFoodBuyOrder(){
		map = new HashMap<String, Object>();
		try {
			int ruleId = mstFood.getRuleId(); // 规则ID
			int goodsType = mstFood.getGoodsType();//产品类型
			
			// 解密爱动号
			mstFood.setIdongId(SecurityUtils.decryptIdongId(mstFood.getIdongIdEncrypt())); // 爱动号
			int idongId = mstFood.getIdongId(); // 爱动号
			String contactName = mstFood.getContactName(); // 联系人姓名
			// 解密联系人电话
			mstFood.setContactPhone(SecurityUtils.decryptPhoneNo(mstFood.getContactPhoneEncrypt()));
			String contactPhone = mstFood.getContactPhone(); // 联系人电话
			String buyDate = mstFood.getBuyDate(); // 订餐日期
			int quantity = mstFood.getQuantity(); // 数量
			if(ruleId > 0 && quantity > 0 && goodsType > 0
					&& (!"".equals(contactName) && contactName != null)
					&& (!"".equals(contactPhone) && contactPhone != null)
					&& (!"".equals(buyDate) && buyDate != null)){
				if (goodsType == 8) {//票务
					return "succ";
				}else {
					String buyYearDate = new SimpleDateFormat("yyyy").format(new Date()) + "-" + buyDate;
					mstFood.setBuyDate(buyYearDate);
					
					// 订单规则信息
					Map<String, Object> paraMap = trnFoodBuyBiz.getOrderRuleInfo(ruleId);//获取供应规则信息
					paraMap.put("idongId", idongId);
					paraMap.put("contactName", contactName);
					paraMap.put("contactPhone", contactPhone);
					paraMap.put("quantity", quantity);
					paraMap.put("buyDate", mstFood.getBuyDate());
					paraMap.put("goodsType", goodsType);
					
					double unitPrice = Double.parseDouble(paraMap.get("price").toString());
					double amount = unitPrice * quantity;
					/*if (selectPickUpWay == 2) {//快递
						amount = amount + CodeConsts.Freight;
						paraMap.put("amount", amount);
					}else {
						paraMap.put("amount", amount);
					}*/
					String province,city,country,detailAddress,address="",receiverName="",mobilePhone="";
					if (selectPickUpWay == 2) {
						//storeId
						paraMap.put("storeId", 1853);//m上1853是爱动健身
						List<Map<String, Object>> addressInfo = new ArrayList<Map<String, Object>>();
						if (selectAddressId == 0) {
							addressInfo = myAddressBiz.getReceiverAddressDefault(idongId);
							if (addressInfo.size() > 0) {
								selectAddressId = Integer.valueOf(addressInfo.get(0).get("autoId").toString());
							}
						}else {
							paraMap.put("autoId", selectAddressId);
							addressInfo = myAddressBiz.getReceiverAddress(paraMap);
						}
						province = addressInfo.get(0).get("province").toString();
						city = addressInfo.get(0).get("city").toString();
						country = addressInfo.get(0).get("country").toString();
						detailAddress = addressInfo.get(0).get("detailAddress").toString();
						address = province + city + country + detailAddress;
						receiverName = addressInfo.get(0).get("receiverName").toString();
						mobilePhone = addressInfo.get(0).get("mobilePhone").toString();
					}
					paraMap.put("amount", amount);
					paraMap.put("pickUpStoreId", selectStoreId);
					paraMap.put("addressId", selectAddressId);
					paraMap.put("pickUpWay", selectPickUpWay);
					paraMap.put("recommendCoach", recommendCoach);
					paraMap.put("address", address);
					paraMap.put("nameAddress", receiverName);
					paraMap.put("mobileAddress", mobilePhone);
					if (goodsType == 3) {
						paraMap.put("orderType", 3);
					}else if (goodsType == 6) {
						paraMap.put("orderType", 4);
					}else if (goodsType == 7) {
						paraMap.put("orderType", 5);
					}
					
					if(idongId > 0) { // 已登录
						trnFoodBuyBiz.addFoodBuy(paraMap);//餐饮购买表插入数据-选中的送货地址ID、选中的取货方式、门店ID
						
						Map<String, Object> resultMap = new HashMap<String, Object>();
						resultMap.put("subscribeId", paraMap.get("subscribeId"));
						resultMap.put("orderId", paraMap.get("orderId"));
						resultMap.put("quantity", quantity);
						resultMap.put("amount", amount);
						resultMap.put("foodName", paraMap.get("tradeName"));
						resultMap.put("brandId", paraMap.get("brandId"));
						
						map.put("code", 1);
						List<Map<String, Object>> payInfo = getPayInfo(mstFoodBiz.getPayInfo(ruleId));
						map.put("zhifubao", payInfo.get(0).get("zhifubao"));
						map.put("weixin", payInfo.get(1).get("weixin"));
						map.put("voucher", payInfo.get(2).get("voucher"));
						map.put("exchange", payInfo.get(3).get("exchange"));
						map.put("membership", payInfo.get(4).get("membership"));
						map.put("discount", payInfo.get(5).get("discount"));
						map.put("result", resultMap);
						return "succ";
					}else{ // 未登录
						// 是否已注册
						int count = mstCampaignBiz.getIfRegister(contactPhone);
						if(count > 0){ // 已注册
							// 直接保存预约信息
							int rsultIdongId = mstCampaignBiz.getIdongIdByPhone(contactPhone);
							paraMap.put("idongId", rsultIdongId);
							trnFoodBuyBiz.addFoodBuy(paraMap);
							
							Map<String, Object> resultMap = new HashMap<String, Object>();
							resultMap.put("subscribeId", paraMap.get("subscribeId"));
							resultMap.put("orderId", paraMap.get("orderId"));
							resultMap.put("quantity", quantity);
							resultMap.put("amount", amount);
							resultMap.put("foodName", paraMap.get("tradeName"));
							
							map.put("code", 1);
							List<Map<String, Object>> payInfo = getPayInfo(mstFoodBiz.getPayInfo(ruleId));
							map.put("zhifubao", payInfo.get(0).get("zhifubao"));
							map.put("weixin", payInfo.get(1).get("weixin"));
							map.put("voucher", payInfo.get(2).get("voucher"));
							map.put("exchange", payInfo.get(3).get("exchange"));
							map.put("membership", payInfo.get(4).get("membership"));
							map.put("discount", payInfo.get(5).get("discount"));
							map.put("result", resultMap);
							return "succ";
						}else{ // 未注册
							// 先注册新用户，产生爱动号
							Map<String, Object> regMap = new HashMap<String, Object>();
							regMap.put("contactName", contactName);
							regMap.put("contactPhone", contactPhone);
							mstCampaignBiz.addMemberReg(regMap);
							// 注册成功后产生爱动号
							int regIdongId = Integer.parseInt(regMap.get("idongId").toString());
							paraMap.put("idongId", regIdongId);
							trnFoodBuyBiz.addFoodBuy(paraMap);
							
							Map<String, Object> resultMap = new HashMap<String, Object>();
							resultMap.put("subscribeId", paraMap.get("subscribeId"));
							resultMap.put("orderId", paraMap.get("orderId"));
							resultMap.put("quantity", quantity);
							resultMap.put("amount", amount);
							resultMap.put("foodName", paraMap.get("tradeName"));
							
							map.put("code", 1);
							List<Map<String, Object>> payInfo = getPayInfo(mstFoodBiz.getPayInfo(ruleId));
							map.put("zhifubao", payInfo.get(0).get("zhifubao"));
							map.put("weixin", payInfo.get(1).get("weixin"));
							map.put("voucher", payInfo.get(2).get("voucher"));
							map.put("exchange", payInfo.get(3).get("exchange"));
							map.put("membership", payInfo.get(4).get("membership"));
							map.put("discount", payInfo.get(5).get("discount"));
							map.put("result", resultMap);
							return "succ";
						}
					}
				}
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	
	/**
	 * 得到支付信息
	 * @param campaignId
	 * @return
	 */
	public List<Map<String, Object>> getPayInfo(String payInfo){
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> zhifubao = new HashMap<String, Object>();
		Map<String, Object> weixin = new HashMap<String, Object>();
		Map<String, Object> voucher = new HashMap<String, Object>();
		Map<String, Object> exchange = new HashMap<String, Object>();
		Map<String, Object> membership = new HashMap<String, Object>();
		Map<String, Object> discount = new HashMap<String, Object>();
		if(payInfo.contains("1")){
			zhifubao.put("zhifubao", true);
			result.add(zhifubao);
		}else{
			zhifubao.put("zhifubao", false);
			result.add(zhifubao);
		}
		if(payInfo.contains("2")){
			weixin.put("weixin", true);
			result.add(weixin);
		}else{
			weixin.put("weixin", false);
			result.add(weixin);
		}
		if(payInfo.contains("3")){
			voucher.put("voucher", true);
			result.add(voucher);
		}else{
			voucher.put("voucher", false);
			result.add(voucher);
		}
		if(payInfo.contains("4")){
			exchange.put("exchange", true);
			result.add(exchange);
		}else{
			exchange.put("exchange", false);
			result.add(exchange);
		}
		if(payInfo.contains("5")){
			membership.put("membership", true);
			result.add(membership);
		}else{
			membership.put("membership", false);
			result.add(membership);
		}
		if(payInfo.contains("6")){
			discount.put("discount", true);
			result.add(discount);
		}else{
			discount.put("discount", false);
			result.add(discount);
		}
		return result;
	}
}
